<?php

include_once("ID3.class.php");
include_once("operacionesBD.php");

class ID3Mapper
{
    public static function findById($id) {
        $dbh = operacionesBD::getInstance('localhost', 'root', '', 'musica');
        $query = "SELECT * from metadata WHERE metadataid = '$id'";
        $data = $dbh->consultar($query);
        if (!$data) return false;
        return new ID3($id, $data[0]['artist'], $data[0]['title'], $data[0]['track'], $data[0]['year'], 
                        $data[0]['album'], $data[0]['file'], $data[0]['userid'], $data[0]['public']);    
    }
    
    public static function findByUserid($userid) {
        $dbh = operacionesBD::getInstance('localhost', 'root', '', 'musica');
        $query = "SELECT * from metadata WHERE userid = '$userid'";
        $data = $dbh->consultar($query);
        if (!$data) return false;
        else {
            foreach ($data as $d) {
                $songs[] = new ID3($d['metadataid'], $d['artist'], $d['title'], $d['track'], $d['year'], 
                        $d['album'], $d['file'], $userid, $d['public']);             
            }
            return $songs;
        }
    }
    
    /**
     * Devuelve todos los ID3 públicos (de todos los usuarios) y
     * los ID3 privados propios.
     */
    
    public static function findPublicID3AndUserPrivateSongs($userid) {
        $dbh = operacionesBD::getInstance('localhost', 'root', '', 'musica');
        $query = "SELECT * from metadata WHERE public = '1' OR (userid = '$userid' AND public = '0')";
        $data = $dbh->consultar($query);
        if (!$data) return false;
        else {
            foreach ($data as $d) {
                $songs[] = new ID3($d['metadataid'], $d['artist'], $d['title'], $d['track'], $d['year'], 
                        $d['album'], $d['file'], $userid, $d['public']);             
            }
            return $songs;
        }       
    }
    
    public function update(ID3 $id3) {
	$public = $id3->get_public();
	$public = $public == 'true' ? 1 : 0; // Para solucionar conflicto de tipos boolean/string
        $dbh = operacionesBD::getInstance('localhost', 'root', '', 'musica'); 
        $query = "UPDATE metadata SET artist = '{$id3->get_artist()}', 
                                      title = '{$id3->get_title()}', 
                                      track = '{$id3->get_track()}',
                                      year = '{$id3->get_year()}', 
                                      album = '{$id3->get_album()}', 
                                      file = '{$id3->get_file()}', 
                                      image = '{$id3->get_image()}', 
                                      public = '{$public}',
                                      userid = '{$id3->get_userid()}' 
                  WHERE metadataid = '{$id3->get_id()}'";
        $data = $dbh->modificar($query);
    }

    public function insert(ID3 $id3) {
        $dbh = operacionesBD::getInstance('localhost', 'root', '', 'musica');
        $query = "INSERT INTO metadata VALUES ('{$id3->get_id()}', '{$id3->get_artist()}', '{$id3->get_title()}', 
                 '{$id3->get_track()}', '{$id3->get_year()}', '{$id3->get_album()}', '{$id3->get_file()}', 
                 '{$id3->get_image()}',  '{$id3->get_public()}', '{$id3->get_userid()}')";
        $id = $dbh->modificar($query);
	return $id;
    }

    public function delete(ID3 $id3) {
        $dbh = operacionesBD::getInstance('localhost', 'root', '', 'musica');
        $query = "DELETE FROM metadata WHERE metadataid = '{$id3->get_id()}'";
        $data = $dbh->modificar($query);
    }
}

?>
